package com.lun.swordtowardoffer2.c03;

public class CheckInclusion {

	public boolean checkInclusion(String s1, String s2) {
		if(s1.length() > s2.length())
			return false;
		
		int[] counts = new int[26];
		for(int i = 0; i < s1.length(); i++) {
			counts[s1.charAt(i) - 'a']++;
			counts[s2.charAt(i) - 'a']--;//相当于双指针右指针
		}
		
		if(areAllZero(counts)) return true;
		
		for(int i = s1.length(); i < s2.length(); i++) {
			counts[s2.charAt(i) - 'a']--;
			counts[s2.charAt(i - s1.length()) - 'a']++;
			
			if(areAllZero(counts)) return true;			
		}
		
		return false;
	}
	
	private boolean areAllZero(int[] counts) {
		for(int count : counts) {
			if(count != 0)
				return false;
		}
		return true;
	}
}
